/*
 * @Title: 姿美汇H5商城编辑收货地址
 * @Author: zhj
 * @Date:   2018-10-13
 * @Last Modified by:   zhj
 * @Last Modified time: 2018-10-18
 */
var dataInfo = new Object(),
	template = {
		init: function() {
			dataInfo = new Vue({
				el: '#app',
				data: {
					show: false,
					addressId: "",
					isDefault: "", //删除是否是默认地址
					type: false, //是否设为默认
					areaList: addressInfo,
					addAdress: { //提交的地址信息
						userId: userId,
						id: "",
						name: '',
						phone: '',
						province: '',
						city: '',
						district: '',
						address: '',
						defaultAddress: 1
					},
				},
				methods: {
					onLoad: function() {
						dataInfo.getAddressDetails();
					},
					choseOver: function(data) { //用户选择地址结束
						this.addAdress.province = data[0].name;
						this.addAdress.city = ' ' + data[1].name + ' ';
						this.addAdress.district = data[2].name;
						this.show = false;
					},
					getAddressDetails: function() { //查看地址详情
						ajaxTemplate(addressTest + SERVICE.getAddressDetails, function(data) {
							var addressdaTa = data.data.result;
							if(data.tag == 0) {
								dataInfo.addAdress.name = addressdaTa[0]['name'];
								dataInfo.addAdress.phone = addressdaTa[0]['phone'];
								dataInfo.addAdress.province = addressdaTa[0]['province'];
								dataInfo.addAdress.city = addressdaTa[0]['city'];
								dataInfo.addAdress.district = addressdaTa[0]['district'];
								dataInfo.addAdress.address = addressdaTa[0]['address'];
								dataInfo.addAdress.id = addressdaTa[0]['id'];
								dataInfo.isDefault = addressdaTa[0]['defaultAddress'];
								if(addressdaTa[0]['defaultAddress'] == 0) {
									dataInfo.type = true
								} else {
									dataInfo.type = false
								}
							} else {

							}
						}, {
							userId: userId,
							id: getQueryString("addressId")
						});
					},
					isdelect: function() { //设为默认地址
						ajaxTemplate(addressTest + SERVICE.getAddressDetails, function(data) {}, {
							userId: userId,
							id: getQueryString("addressId")
						});
					},
					editAddress: function() { //修改收货地址
						if(!this.addAdress.name || !this.addAdress.name.trim()) {
							this.$toast('请输入收货人姓名');
						} else if(!this.addAdress.phone) {
							this.$toast('请输入收货人手机号');
						} else if(!(/^1[0-9]\d{9}$/.test(this.addAdress.phone))) {
							this.$toast('请输入正确的手机号');
						} else if(!this.addAdress.district) {
							this.$toast('请选择收货区域');
						} else if(!this.addAdress.address || !this.addAdress.address.trim()) {
							this.$toast('请输入详细地址');
						} else {
							this.$toast.loading({
								forbidClick: true,
								message: '收货地址保存中...',
								loadingType: 'spinner',
								duration: 0,
							});
							if(dataInfo.type){
								dataInfo.addAdress.defaultAddress = 0
							}
							ajaxTemplate(addressTest + SERVICE.updAddress, function(data) {
								if(data.tag == 0) {
									location.href = '../html/addressList.html?from=' + getQueryString('from') + '&addressId=' + dataInfo.addAdress.id+'&buyWay='+getQueryString('buyWay');
								} else {
									dataInfo.$toast.clear();
									dataInfo.$toast(data.errMsg);
								};
							}, JSON.parse(JSON.stringify(this.addAdress)));
						};
					},
					delectAddress: function() {
						this.$dialog.confirm({
							message: '确认删除该地址吗？'
						}).then(() => { //确认
							console.log("确认");
							ajaxTemplate(addressTest + SERVICE.delAddress, function(data) {
								if(data.tag == 0) {
									location.href = '../html/addressList.html?from=' + getQueryString('from') + "&addressId=" + dataInfo.addAdress.id+'&buyWay='+getQueryString('buyWay');
								} else {}
							}, {
								userId: userId,
								id: getQueryString("addressId"),
								isDefault: dataInfo.isDefault
							});

						}).catch(() => { //取消
							console.log("取消");
						});
					}
				}
			});
			dataInfo.onLoad();
		}
	};
template.init();